package cn.dogeye.controller;


import cn.dogeye.common.protocol.Result;
import cn.dogeye.entity.SysAuth;
import cn.dogeye.service.SysAuthService;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys:auth")
public class SysAuthController {

    @Autowired
    SysAuthService authService;

    /**
     *(根据id)获得所有相关权限
     * @param id
     * @return
     */
    @GetMapping("/getAuth")
    public Result getAuth(Integer id){
        List<SysAuth> list = authService.getAuth(id);
        return Result.succ(list);
    }

    @GetMapping("/delAuth")
    public Result delAuth(String ids){
        try {
            List idsList=new ArrayList();
            String [] authIds=ids.split(",");
            for (String authId : authIds) {
                idsList.add(authId);
            }
            authService.delAuthByIds(idsList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败");
        }
        return Result.succ("删除成功",1);
    }

    @PostMapping("/saveAuth/{authForm}")
    public Result saveAuth(@PathVariable("authForm") String authForm){
        try {
            SysAuth sysAuth = JSONUtil.toBean(authForm, SysAuth.class);
            authService.addSysAuth(sysAuth);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("保存失败");
        }
        return Result.succ("保存成功",1);
    }

    @GetMapping("/getAuthById")
    public Result getAuthById(Integer id){
        return Result.succ(authService.getById(id));
    }

    @PostMapping("/updateAuth/{authForm}")
    public Result updateAuth(@PathVariable("authForm")String authForm){
        try {
            SysAuth sysAuth = JSONUtil.toBean(authForm, SysAuth.class);
            authService.saveOrUpdate(sysAuth);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新失败");
        }
        return Result.succ("更新成功",1);
    }

}
